package interview;

public class interview_17_10 {
    public int majorityElement(int[] nums) {
        int number=0;
        int time=0;
        int left=0;
        int right=nums.length-1;
        while(left<right){
            if(nums[left]==nums[right]){
                if(nums[left]==number){
                    time+=2;
                }else if(time==0){
                    number=nums[left];
                    time=2;
                }else{
                    time-=2;
                }
            }
            left++;
            right--;
        }
        if(time>=2){
            return number;
        }else{
            if(left!=right) return -1;
            number=nums[left];
            time=0;
            for(int i=0;i<nums.length;++i){
                if(nums[i]==number) time++;
            }
            if(time>nums.length/2) return number;
            else return -1;
        }
    }
}
